﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Business.Inv;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageDeliveryOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private SalesDeliveryOrderEntity mSelectedDeliveryOrder;
        #endregion

        #region ctors
        public frmManageDeliveryOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "DO", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colDELIVERY_TYPE, "DW", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colTRANSPORT_TYPE, "TF", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colDELIVERY_STATUS, "DO", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPACKAGE_TYPE, "PT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colDELIVERY_APPLICATION, "DA", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUNIT, "UNIT", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND DO.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (string.IsNullOrEmpty(txtDeliveryOrderCode.Text) == false)
            {
                condition += " AND DO.DELIVERY_ORDER_CODE LIKE '%" + txtDeliveryOrderCode.Text + "%'";
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                condition += " AND DO.DELIVERY_DATE >= CONVERT(DATETIME,'" + dtpDeliveryDate.EditValue.ToString() + "')";
            }
            if (dtpDeliveryDateTo.EditValue != null)
            {
                condition += " AND DO.DELIVERY_DATE <= CONVERT(DATETIME,'" + dtpDeliveryDateTo.EditValue.ToString() + "')";
            }
            if (txtDeliveryPerson.Tag != null && txtDeliveryPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtDeliveryPerson.Tag;
                condition += " AND DO.DELIVERY_PERSON = '" + entity.PersonId + "' ";
            }
            else
            {
                if (!mCanQueryOtherPerson)
                {
                    condition += " AND DO.DELIVERY_PERSON = '" + CommonParameter.CurrentUser.PersonId + "' ";
                }
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND DO.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "DELIVERY_ORDER_CODE DESC";
            DataTable dt = SalesDeliveryOrderBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void BindDetailGrid(string deliveryId)
        {
            if (string.IsNullOrEmpty(deliveryId))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = SalesDeliveryItemBs.GetDataSetByDeliveryID(deliveryId);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnDelivery.Enabled = false;
            btnFeedBack.Enabled = false;
            btnBilling.Enabled = false;
            if (mSelectedDeliveryOrder == null)
            {
                return;
            }
            if (mSelectedDeliveryOrder.Status == DeliveryOrderStatus.DO01.ToString()
                || mSelectedDeliveryOrder.Status == DeliveryOrderStatus.DO02.ToString()
                || mSelectedDeliveryOrder.Status == DeliveryOrderStatus.DO04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedDeliveryOrder.Status == DeliveryOrderStatus.DO03.ToString())
            {
                btnDelivery.Enabled = true;
            }
            else if (mSelectedDeliveryOrder.Status == DeliveryOrderStatus.DO05.ToString())
            {
                btnFeedBack.Enabled = true;
                btnBilling.Enabled = true;
            }
        }
        #endregion

        #region evnets
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.销售);
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedDeliveryOrder = new SalesDeliveryOrderEntity(dr);
            BindDetailGrid(mSelectedDeliveryOrder.DeliveryOrderId);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDeliveryOrder frm = new frmMaintainDeliveryOrder();
            frm.ThisActionType = ActionType.View;
            frm.SelectedDeliveryOrder = mSelectedDeliveryOrder;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中发货申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedDeliveryOrder.Status = DeliveryOrderStatus.DO07.ToString();
                mSelectedDeliveryOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedDeliveryOrder.LastModifiedDate = DateTime.Now;
                bool successful = SalesDeliveryOrderBs.UpdateSalesDeliveryOrder(mSelectedDeliveryOrder);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中发货申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedDeliveryOrder.Status = DeliveryOrderStatus.DO02.ToString();
                mSelectedDeliveryOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedDeliveryOrder.LastModifiedDate = DateTime.Now;
                bool successful = SalesDeliveryOrderBs.UpdateSalesDeliveryOrder(mSelectedDeliveryOrder);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDeliveryOrder frm = new frmMaintainDeliveryOrder();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedDeliveryOrder = mSelectedDeliveryOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDeliveryOrder frm = new frmMaintainDeliveryOrder();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedDeliveryOrder = mSelectedDeliveryOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtDeliveryPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtDeliveryPerson.Text = frm.SelectedPerson.NameCn;
                txtDeliveryPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtDeliveryPerson.Text = "";
                txtDeliveryPerson.Tag = null;
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void btnDelivery_Click(object sender, EventArgs e)
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否发货选中发货申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedDeliveryOrder.Status = DeliveryOrderStatus.DO05.ToString();
                mSelectedDeliveryOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedDeliveryOrder.LastModifiedDate = DateTime.Now;
                //bool successful = SalesDeliveryOrderBs.UpdateSalesDeliveryOrder(mSelectedDeliveryOrder);
                bool successful = InvDispatchOrderBs.SalesOrderDelivery(mSelectedDeliveryOrder, CommonParameter.CurrentUser.Password);
                if (successful)
                {
                    MessageBox.Show("发货成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnBilling_Click(object sender, EventArgs e)
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<SalesDeliveryItemEntity> list = SalesDeliveryItemBs.GetDeliveryItemByDeliveryID(mSelectedDeliveryOrder.DeliveryOrderId);
            MstCustomerEntity customer = MstCustomerBs.GetMstCustomerByID(mSelectedDeliveryOrder.CustomerId);
            Finance.frmMaintainInvoicingApplication frm = new Finance.frmMaintainInvoicingApplication(list, customer);
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        private void btnFeedBack_Click(object sender, EventArgs e)
        {
            if (mSelectedDeliveryOrder == null)
            {
                MessageBox.Show("请选择一个发货申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainDeliveryFeedback frm = new frmMaintainDeliveryFeedback(mSelectedDeliveryOrder);
            SalesDeliveryFeedbackEntity feadback = SalesDeliveryFeedbackBs.GetSalesDeliveryFeedbackByDeliveryId(mSelectedDeliveryOrder.DeliveryOrderId);
            if (feadback == null)
            {
                frm.ThisActionType = ActionType.New;
            }
            else
            {
                frm.ThisActionType = ActionType.Modify;
                frm.SelectedFeedback = feadback;
            }
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        #endregion

    }
}
